package com.whcoding.jpa.controller;

import com.whcoding.jpa.pojo.OrmDeptPO;
import com.whcoding.jpa.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Manager
 */
@RestController
@RequestMapping("/api/dept/")
public class DeptController {

	private static final Logger log = LoggerFactory.getLogger(DeptController.class);

	@Autowired
	private DeptService deptService;

	@PostMapping("add")
	public ResponseEntity addDept(OrmDeptPO deptVO) {
		return ResponseEntity.ok(deptService.addDept(deptVO));
	}

	/**
	 * http://localhost:9110/api/dept/queryDeptById/1
	 * REST ful 风格
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("queryDeptById/{id}")
	public ResponseEntity queryDeptById(@PathVariable("id") Integer id) {
		return ResponseEntity.ok(deptService.queryDeptById(id));
	}


	/**
	 * 查询全部数据
	 * http://localhost:9110/api/dept/queryAllDept
	 * @return
	 */
	@GetMapping("queryAllDept")
	public ResponseEntity queryAllDept() {
		return ResponseEntity.ok(deptService.queryAllDept());
	}


	/**
	 * ResponseEntity 返回数据
	 * 根据DeptName 查询数据
	 *
	 * @param deptName
	 * @return
	 */
	@GetMapping("queryByDeptName")
	public ResponseEntity queryByDeptName(@RequestParam("deptName") String deptName) {
		return ResponseEntity.ok(deptService.queryDeptByDeptName(deptName));
	}

	/**
	 * 根据Sql查询
	 *
	 * @return
	 */
	@GetMapping("queryDeptBySql")
	public ResponseEntity queryDeptBySql() {
		return ResponseEntity.ok(deptService.queryDeptBySql());
	}

	@GetMapping("/deleteByDeptId")
	public void deleteByDeptId(Long deptId) {
		log.info("开始删除数据deptid={}",deptId);
		deptService.deleteByDeptId(deptId);
		log.info("结束删除数据deptid={}",deptId);
	}

}
